#include "ISPMCommon.h"
#include "D3DDevice.h"

HRESULT CD3DDevice::InitDevice(const DXGI_SWAP_CHAIN_DESC* SwapChainDesc)
{
    HRESULT hr = S_OK;

    m_bPresentable = SwapChainDesc != NULL;

    // setting the device flags
    UINT createDeviceFlags = 0;
#ifdef _DEBUG
    createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

    // feature levels
    D3D_FEATURE_LEVEL featureLevels[] =
    {
        D3D_FEATURE_LEVEL_11_0,
        D3D_FEATURE_LEVEL_10_1,
        D3D_FEATURE_LEVEL_10_0,
    };
    UINT numFeatureLevels = ARRAYSIZE( featureLevels );

    //driver type
    D3D_DRIVER_TYPE DriverType = m_bPresentable ? D3D_DRIVER_TYPE_HARDWARE : D3D_DRIVER_TYPE_WARP;

    if(m_bPresentable) {
        hr = D3D11CreateDeviceAndSwapChain( NULL, DriverType, NULL, createDeviceFlags, featureLevels, numFeatureLevels,
            D3D11_SDK_VERSION, SwapChainDesc, &m_pSwapChain, &m_pd3dDevice, &m_featureLevel, &m_pImmediateContext );
    }
    else {
        hr = D3D11CreateDevice(NULL, DriverType, NULL, createDeviceFlags, featureLevels, numFeatureLevels, 
            D3D11_SDK_VERSION, &m_pd3dDevice, &m_featureLevel, &m_pImmediateContext);
    }

    return hr;

}

CD3DDevice::CD3DDevice() : m_featureLevel(D3D_FEATURE_LEVEL_11_0), m_pd3dDevice(NULL), m_pImmediateContext(NULL), m_pSwapChain(NULL)
{

}
